# coding: utf-8

from sqlalchemy import Column, Integer, String, Text
from sqlalchemy.orm import declarative_base
from sqlalchemy import create_engine
from settings import MYSQL_HOST, MYSQL_PASSWORD, MYSQL_USERNAME, MYSQL_PORT,\
    MYSQL_DB

# 实例化db 对象
db = create_engine(
    f"mysql+pymysql://{MYSQL_USERNAME}:{MYSQL_PASSWORD}@{MYSQL_HOST}:"
    f"{MYSQL_PORT}/{MYSQL_DB}?charset=utf8mb4"
)
Base = declarative_base()


class UserInfos(Base):
    __tablename__ = 'userinfos'
    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(32), nullable=False, unique=True)
    password = Column(String(32), nullable=False)
    crawler_id = Column(Text, nullable=True)

    def __repr__(self):
        return 'UserInfos ---> id:{} username:{}'.format(self.id, self.username)


def init_db():
    """
        创建数据库中不存在的表, 不会更新现有表
    """
    Base.metadata.create_all(db)


def drop_db():
    """
        删除表
    """
    Base.metadata.drop_all(db)


if __name__ == '__main__':
    init_db()
    # drop_db()